/**
 * muggle 文本编辑器 1.0
 */
(function(window, document) {
    "use strict"
    "fuck IE"

    var getSelectedText = function() {
        if (window.getSelection) {
            return window.getSelection().toString();
        } else if (document.getSelection) {
            return document.getSelection();
        } else if (document.selection) {
            return document.selection.createRange().text;
        } else{
            return null;
        }
    }
    var that = {};
    function Note(json) {
        that.data = json;
        var dom = json.dom  // 容器
        var noteFrame = document.createElement("div");  // 主体
        var noteToolbar = document.createElement("div");  // 工具条
        var noteTextarea = document.createElement("textarea");  // 编辑区

        noteFrame.className = "muggle-note-frame";
        noteToolbar.className = "muggle-note-toolbar";
        noteTextarea.className = "muggle-note-textarea";

        // 样式调整
        noteTextarea.style.minHeight = json.minHeight + "px";

        // 与 dom 对接
        noteFrame.appendChild(noteToolbar);
        noteFrame.appendChild(noteTextarea);
        dom.appendChild(noteFrame);

        // 工具条项目
        var toolTextB = document.createElement("button");
        var toolTextI = document.createElement("button");
        var toolTextU = document.createElement("button");
        var toolTextImg = document.createElement("button");
        toolTextB.type = "button";
        toolTextB.className = "muggle-note-tool-text"
        toolTextB.innerText = "B";
        toolTextI.type = "button";
        toolTextI.className = "muggle-note-tool-text"
        toolTextI.innerText = "I";
        toolTextU.type = "button";
        toolTextU.className = "muggle-note-tool-text"
        toolTextU.innerText = "U";
        toolTextImg.type = "button";
        toolTextImg.className = "muggle-note-tool-text"
        toolTextImg.innerText = "图";
        noteToolbar.appendChild(toolTextB);
        noteToolbar.appendChild(toolTextI);
        noteToolbar.appendChild(toolTextU);
        noteToolbar.appendChild(toolTextImg);

        // 高度自适应
        var swap_height = json.minHeight;
        var initScroll = noteTextarea.scrollHeight;
        noteTextarea.addEventListener("input", function(event) {
            if (initScroll < this.scrollHeight) {
                noteTextarea.style.minHeight = this.scrollTop + swap_height + "px";
            }
        });

        // 嵌套标签getSelectedText
        var registTextFunc = function(text_dom, sign) {
            text_dom.addEventListener("mousedown", function(event) {
                var text = getSelectedText();
                if (text != null) {
                    var swap_text = noteTextarea.value;
                    var index = noteTextarea.selectionStart;  // 光标索引
                    var fronttext = swap_text.substr(0, index);
                    var endtext = swap_text.substr(index+text.length);
                    text = "<"+sign+">"+text+"</"+sign+">";
                    noteTextarea.value = fronttext+text+endtext;
                }
            });
        };
        // 图片按钮
        toolTextImg.addEventListener("mousedown", function(event) {
            // 文件切分
            var swap_text = noteTextarea.value;
            var index = noteTextarea.selectionStart;  // 光标索引
            var fronttext = swap_text.substr(0, index);
            var endtext = swap_text.substr(index);
            setTimeout(function() {
                window.Muggle.AjaxFile({
                    url: "/cgi/?cmd=FileLink&app=save",
                    sign: "blldxt_"+(new Date()).valueOf(),  // 标识，用户id+时间戳
                    type: "image",
                    success: function(data) {
                        noteTextarea.focus();
                        if (data.code == 0) {
                            var text = "<img src=\""+data.data.src+"\" />";
                            noteTextarea.value = fronttext+text+endtext;
                        }
                    }
                });
            }, 300);
        });

        noteTextarea.addEventListener("keydown", function(event) {
            if (event && event.keyCode==13) {
                this.value += "<br />";
            }
        });
        registTextFunc(toolTextB, "strong");
        registTextFunc(toolTextI, "i");
        registTextFunc(toolTextU, "u");

        this.Content = function() {
            var r = String.fromCharCode(13);
            var n = String.fromCharCode(10);
            var reg1 = new RegExp(r+n);
            var reg2 = new RegExp(n);
            noteTextarea.value.replace(reg1, "<br />");
            noteTextarea.value.replace(reg2, "<br />");
            return noteTextarea.value;
        };
    }


    window.MuggleNote = Note;
})(window, document)